<?php

namespace addons\hlapp\controller;
use addons\hlapp\dto\Result;
use addons\hlapp\model\HlappProject;
use addons\hlapp\model\HlappProjectEvaluate;
use addons\hlapp\model\HlappProjectStatus;
use addons\hlapp\service\CacheService;
use addons\hlapp\service\PushService;

class Admin extends Common{
    public $adminLogin=true;//注意：因为这是后台业务菜单所以需要管理员登录操作的
    public $pageSize = 10;

//-------------
//---- 页面
//-------------
    public function index(){
        return $this->fetch();
    }

    /**
     * 桌面
     * @return mixed|void
     */
    public function desktop(){
        $system['php_version'] = PHP_VERSION;
        $system['zend_version'] = zend_version();
        $system['php_os'] = PHP_OS;
        $system['server_software'] =  $_SERVER['SERVER_SOFTWARE'];
        $system['php_sapi_name'] = php_sapi_name();
        $system['SERVER_PORT']=getenv('SERVER_PORT');
        $system['time']=date('Y-m-d H:i');
        $system['weeks']=$this->getWeekOfYear(time());
        $this->assign("system",$system);
        return $this->fetch();
    }
    //清除缓存
    public function clearnCache(){
        CacheService::clearnCache();
    }

    // 项目列表
    public function projectList(){
        return $this->fetch();
    }

    // 工程评价页面
    public function evaluate(){
        $projectId = $_GET["projectId"];
        $eModel = new HlappProjectEvaluate();
        $pModel = new HlappProject();
        $list = $eModel->evaluateList($projectId);
        $project = $pModel->where('id', $projectId) ->find();
        $this->assign("Elist",$list);
        $this->assign("project",$project);
        return $this->fetch();
    }

    // 测试页面
    public function test(){
        $keywords = array("name"=>"海骊住建");
        foreach($keywords as $key=>$value){
            $_POST["keywords"] ="asdf";
           dump($_POST);
        }

    }

    /**
     * 添加项目页面
     * @return mixed|void
     */
    public function addProject(){
        return $this->fetch();
    }

    /**
     * 修改项目页面
     * @return mixed|void
     */
    public function updateProject(){
        $projectId = $_GET["projectId"];
        $pModel = new HlappProject();
        $project = $pModel->where('id', $projectId) ->find();
        $this->assign("project",$project);
        return $this->fetch();
    }

    /**
     * 修改工程状态页面
     */
    public function changeStatusPage(){
        $projectId = $_GET["projectId"];
        $pModel = new HlappProject();
        $sModel =  new HlappProjectStatus();
        $project = $pModel->where('id', $projectId) ->find();
        $status = $sModel->order(['sort'=>'asc','id'=>'asc'])->select();
        $this->assign("project",$project);
        $this->assign("status",$status);
        return $this->fetch();
    }

//-------------
//---- 数据
//-------------
    /**
     * 给负责人发送评价消息
     */
    public function sendEvaluateToMember(){
        $r = null;
        $projectId = $_POST["projectId"];
        $statusId = $_POST["statusId"];
        $projectModel = new \addons\hlapp\model\HlappProject();
        $statusModel = new \addons\hlapp\model\HlappProjectStatus();
        $project = $projectModel->where("id",$projectId)->find();
        $status = $statusModel->where("id",$statusId)->find();
        $openId = $project["openId"];

        $templateId="62-5mIeaX7rCwroahhD8i1rSBZZOdCz06PK8P1qACbA";
        $jumpUrl=addonUrl("hlapp/Index/evaluate",'',true,true);
        $jumpUrl = $jumpUrl."?projectId=".$projectId."&statusId=".$statusId;
        $data=[
            "first"=>["value"=>$project["name"]."进度评价","color"=>"#173177"],
            "keyword1"=>["value"=>$project["name"],"color"=>"#173177"],
            "keyword2"=>["value"=>"2018-01-30","color"=>"#173177"],
            "keyword3"=>["value"=>$status["description"],"color"=>"#173177"],
            "remark"=>["value"=>"点击进行评价","color"=>"#173177"]
        ];

        if(!$openId){
            $r = Result::getError("请负责人先进行信息登记！");
        }else{
            $result = (new PushService())->sendTemplateMessage($openId,$templateId,$jumpUrl,$data);
            if ($result->code){
                $r = Result::getOk($result);
            }else{
                $r = Result::getResultError();
                $r->setResult($result);
                $r = $r->toJson();
            }
        }
        echo $r;
    }

    /**
     * 保存用户信息
     */
    //        file_put_contents("logTest.txt", json_encode($result),FILE_APPEND);
    public function addMemberInfo(){
        $name = $_POST["username"];
        $telphone = $_POST["telphone"];
        $member = new \addons\hlapp\model\HlappMember();
        $member->addMember($name,$telphone);
        echo json_encode($name);
    }

    /**
     * 获取项目列表
     * @return string
     */
    public function getProjectList(){
        $pageIndex = $_POST["pageIndex"];
        if(array_key_exists('pageMaxSize',$_POST)){
            $pageSize = $_POST['pageMaxSize'];
        }else{
            $pageSize = $this->pageSize;
        }
        $keyword = "";
        if(array_key_exists("keyword", $_POST)){
            $keyword = $_POST["keyword"];
            if($keyword && $keyword != ''){
                $keyword = array("name"=>$keyword);
            }
        }

        if ($pageIndex == null && $pageIndex < 1) {
            $pageIndex = 1;
        }
        $projectModel = new \addons\hlapp\model\HlappProject();
        $result = Result::getOk($projectModel->projectList($pageIndex,$pageSize,$keyword));
        return $result;
    }

    /**
     * 添加项目
     */
    public function addHlProject(){
        $projectModel = new \addons\hlapp\model\HlappProject();
        echo json_encode($projectModel->addProject());
    }

    /**
     * 删除项目
     */
    public function removeProject(){
        $id = $_POST["id"];
        $projectModel = new \addons\hlapp\model\HlappProject();
        $projectModel->where("id",$id)->setField("del",1);
        $projectModel->update();
        echo Result::getOk();
    }

    /**
     * 修改项目
     */
    public function updateHlProject(){
        $id = $_POST["id"];
        $projectModel = new \addons\hlapp\model\HlappProject();
        $projectModel->allowField(['name','manager','developers','developersCharge','chargePhone','position']);
        $r = $projectModel->save($_POST,['id'=>$id]);
        echo Result::getOk($r);
    }

    /**
     * 修改项目状态
     */
    public function changeStatus(){
        $projectModel = new \addons\hlapp\model\HlappProject();
        $status  = $_POST["status"];
        $projectId  = $_POST["projectId"];
        $r = Result::getError("修改失败");
        if($projectId){
            if($status){
                $projectModel->where("id",$projectId)->setField("statusId",$status);
                $projectModel->update();
                $r = Result::getOk();
            }
        }
        return $r;
    }

//    /**
//     * 速普物流单号登记
//     * @return false|string
//     */
//    public function testSupu(){
////        key是订单号  value 是物流单号
////        $delvery["15384934971"]="7700083563617";
//
//        $res = array();
//        foreach ($delvery as $k=>$v){
//            //物流单号信息 提前写入
//            $jsonData = '{"orderCode":'.$k.',"userId":355,"status":"1","sellerCode":"304","deliveryCom":"yunda","deliveryNum":'.$v.'}';
//            $r = $this->http_post_data('http://callback.service.supumall.com/spservice/open/interface/rest/HZSPInterface/updateOrderStatus',$jsonData);
//            array_push($res,$r);
//        }
//        $result = json_encode($r);
//        return $result ;
//    }
}